package io.sage.modules.basics.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import io.sage.modules.basics.dao.BGoodsAllocationMapper;
import io.sage.modules.basics.dao.BGoodsShelvesMapper;
import io.sage.modules.basics.entity.BGoodsAllocation;
import io.sage.modules.basics.vo.CascaderVo;
import io.sage.modules.basics.dao.BWarehouseMapper;
import io.sage.modules.basics.entity.BGoodsShelves;
import io.sage.modules.basics.entity.BWarehouse;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import io.sage.modules.basics.dao.BGoodsAreaMapper;
import io.sage.modules.basics.entity.BGoodsArea;
import io.sage.modules.basics.service.BGoodsAreaService;

@Service("goodsAreaService")
public class BGoodsAreaServiceImpl implements BGoodsAreaService {
	
	@Autowired
	private BGoodsAreaMapper goodsAreaMapper;
	@Autowired
	private BWarehouseMapper warehouseMapper;

	@Autowired
	private BGoodsShelvesMapper goodsShelvesMapper;
	@Autowired
	private BGoodsAllocationMapper goodsAllocationMapper;

	@Override
	public List<BGoodsArea> selectGoodsAreaList(Map<String, Object> params) {
		return goodsAreaMapper.selectGoodsAreaList(params);
	}

	@Override
	public int deleteBatchIds(Integer[] ids) {
		return goodsAreaMapper.deleteBatchIds(ids);
	}

	@Override
	public BGoodsArea selectByPrimaryKey(Integer id) {
		
		return goodsAreaMapper.selectByPrimaryKey(id);
	}

	@Override
	public int insert(BGoodsArea goodsArea) {
		goodsArea.setCreateTime(new Date());
		goodsArea.setUpdateTime(new Date());
		goodsArea.setIsDel(0);
		goodsArea.setIsUsed(1);
		BWarehouse warehouse= warehouseMapper.selectByPrimaryKey(goodsArea.getWareHouseId());
		int num=Integer.parseInt(warehouse.getBarCode());
		SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyyMMddHHmmss");
		String date=simpleDateFormat.format(new Date());
		goodsArea.setAreaCode("HQ"+num+"-"+warehouse.getDepartmentId()+"-"+date);
		goodsArea.setBarCode(date);
		int i=goodsAreaMapper.insertSelective(goodsArea);

		return i;
	}

	@Override
	public int updateByPrimaryKeySelective(BGoodsArea goodsArea) {
		goodsArea.setUpdateTime(new Date());
		return goodsAreaMapper.updateByPrimaryKeySelective(goodsArea);
	}

	@Override
	public List<BGoodsArea> selectIsUse(Integer[] ids) {
		return this.goodsAreaMapper.selectIsUse(ids);
	}

	@Override
	public List<BGoodsArea> getTreeList(Map<String, Object> params) {
		params.put("isUsed", 1);
		params.put("orderby", "ga.bar_code");
		List<BGoodsArea> selectGoodsAreaList = this.goodsAreaMapper.selectGoodsAreaList(params);
		params.put("orderby", "gs.shelve_code");
		List<BGoodsShelves> selectShelvesList = this.goodsShelvesMapper.selectGoodsShelvesList(params);
		params.put("isUsed1", 1);
		params.put("isUsed2", 2);
		params.put("orderby", "a.id");
		List<BGoodsAllocation> selectGoodsAllocationList = this.goodsAllocationMapper.selectGoodsAllocationList(params);
		Map<Integer,List<BGoodsAllocation>> map=selectGoodsAllocationList.parallelStream().collect(Collectors.groupingBy(BGoodsAllocation::getGoodsShelveId));
		for (int i = 0; i < selectShelvesList.size(); i++) {
			selectShelvesList.get(i).setChildren(map.get(selectShelvesList.get(i).getId()));
		}
		Map<Integer,List<BGoodsShelves>> map2=selectShelvesList.parallelStream().collect(Collectors.groupingBy(BGoodsShelves::getGoodsAreaId));
		for (int i = 0; i < selectGoodsAreaList.size(); i++) {
			selectGoodsAreaList.get(i).setChildren(map2.get(selectGoodsAreaList.get(i).getId()));
		}

		return selectGoodsAreaList;
	}

}
